package com.kingsoft.dc.khaos.module.spark.util

import java.util.Properties

import com.kingsoft.dc.khaos.module.spark.constants
import com.kingsoft.dc.khaos.KhaosContext
import com.kingsoft.dc.khaos.extender.meta.utils.DmMetaUtils
import com.kingsoft.dc.khaos.module.spark.constants.CosConstants
import com.kingsoft.dc.khaos.module.spark.model.cos.CosAccessConfig
import com.qcloud.cos.{COSClient, ClientConfig}
import com.qcloud.cos.auth.BasicCOSCredentials
import com.qcloud.cos.model.{CannedAccessControlList, CreateBucketRequest}
import com.qcloud.cos.region.Region

/**
  * Created by jing on 19/5/4.
  */
object CosApiUtils {


  def getHDFSInfoFromFile(): Properties ={
    val properties = FileConfigReader.load(CosConstants.HDFS_CONFIG_FILE)
    properties
  }

  def getCosAccessFromApi(kc: KhaosContext): CosAccessConfig = {
    val cosConfig = getCosAccessAkSk(kc)
    val properties = FileConfigReader.load(CosConstants.COS_CONFIG_FILE)
    cosConfig.setRegion(properties.getProperty(CosConstants.COS_REGION))
    cosConfig.setEndPoint(properties.getProperty(CosConstants.COS_ENDPOINT))
    cosConfig.setBucket(properties.getProperty(CosConstants.COS_BUCKET) + "-" + cosConfig.getAppId)
    cosConfig.setWorkingDir(properties.getProperty(CosConstants.COS_WORKDIR))
    cosConfig.setDelimeter(properties.getProperty(CosConstants.COS_DELIMETER))
    createBucketIfNotExist(cosConfig)
    cosConfig
  }

  def getCosAccessAkSk(kc: KhaosContext): CosAccessConfig = {
    val cosConfig = new CosAccessConfig
    val dmRquest = TenantUtils.buildAkskRequest(kc)
    val tenantInfo = DmMetaUtils.getTenantAkSk(dmRquest)
    val appid = tenantInfo.getAppId
    val ak = tenantInfo.getAk
    val sk = tenantInfo.getSk

    if (appid.isEmpty) {
      throw new Exception("COS APPID 获取异常")
    } else if (ak.isEmpty) {
      throw new Exception("COS AK 获取异常")
    } else if (sk.isEmpty) {
      throw new Exception("COS SK 获取异常")
    } else {
      cosConfig.setAppId(appid)
      cosConfig.setAccessKey(ak)
      cosConfig.setSecretKey(sk)
    }
    cosConfig
  }

  //  def getBucketAndEndpoint(cosAccessConfig: CosAccessConfig) = {
  //    val properties = FileConfigReader.load(CosConstants.COS_CONFIG_FILE)
  //  }

  //  def getCosAccess(config: Configuration): CosAccessConfig = {
  //    val cosConfig = new CosAccessConfig
  //    val jobConf = config.getSparkConf()
  //    if (jobConf.contains(CosConstants.COS_BUCKET)) {
  //      cosConfig.setAccessKey(CosConstants.COS_AK)
  //      cosConfig.setSecretKey(CosConstants.COS_SK)
  //      cosConfig.setAppId(jobConf.get(CosConstants.COS_APPID).get)
  //      cosConfig.setRegion(jobConf.get(CosConstants.COS_REGION).get)
  //      cosConfig.setEndPoint(jobConf.get(CosConstants.COS_ENDPOINT).get)
  //      cosConfig.setBucket(jobConf.get(CosConstants.COS_BUCKET).get + "-" + cosConfig.getAppId)
  //    }
  //
  //    createBucketIfNotExist(cosConfig)
  //    cosConfig
  //  }


  def createBucketIfNotExist(cosAccessConfig: CosAccessConfig): Boolean = {
    var createFlag = false
    // 1 初始化用户身份信息(appid, secretId, secretKey)// 1 初始化用户身份信息(appid, secretId, secretKey)
    val cred = new BasicCOSCredentials(cosAccessConfig.getAccessKey, cosAccessConfig.getSecretKey)
    // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
    val clientConfig = new ClientConfig(new Region(cosAccessConfig.getRegion))
    //    clientConfig.setEndpointBuilder(new CosEndpointBuilder(new Region(cosAccessConfig.getRegion),cosAccessConfig.getEndPoint))
    clientConfig.setEndPointSuffix(cosAccessConfig.getEndPoint)
    // 3 生成cos客户端
    val cosclient = new COSClient(cred, clientConfig)
    val bucketName = cosAccessConfig.getBucket
    if (!cosclient.doesBucketExist(bucketName)) {
      val createBucketRequest = new CreateBucketRequest(bucketName)
      // 设置bucket的权限为PublicRead(公有读私有写), 其他可选Private(私有读写), PublicReadWrite(公有读写)
      createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead)
      val bucket = cosclient.createBucket(createBucketRequest)
      createFlag = true
    }
    createFlag
  }
}
